package com.bawei.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.auth.form.LoginResponse;
import com.bawei.auth.form.UserLogin;
import com.bawei.common.core.domain.Result;
import com.bawei.system.domain.SysUser;
import com.bawei.system.domain.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.bawei.auth.form.LoginBody;
import com.bawei.auth.form.RegisterBody;
import com.bawei.auth.service.SysLoginService;
import com.bawei.common.core.domain.R;
import com.bawei.common.core.utils.JwtUtils;
import com.bawei.common.core.utils.StringUtils;
import com.bawei.common.security.auth.AuthUtil;
import com.bawei.common.security.service.TokenService;
import com.bawei.common.security.utils.SecurityUtils;
import com.bawei.system.domain.model.LoginUser;

/**
 * token 控制
 *
 * @author bawei
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 手机号登录
     * @param form
     * @return
     */
    @PostMapping("loginPhone")
    public R<?> loginPhone(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.loginPhone(form.getPhonenumber(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }


    /**
     * 根据手机号发送验证码
     * @param phone
     * @return
     */
    @GetMapping(value = {"sendMsg/{phone}","sendMsg"})
    public R<String> sendMsg(@PathVariable(value = "phone",required = false) String phone) {
        return R.ok(sysLoginService.sendMsg(phone));
    }


    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }


    /**
     * 新框架登录
     * @param user
     * @return
     */
    @PostMapping("userLogin")
    public Result<LoginResponse> userLogin(@RequestBody UserLogin user)
    {
        // 用户登录
        Result<LoginResponse> loginResponse = sysLoginService.userLogin(user.getUsername(), user.getPassword());
        return Result.ok(loginResponse.getData());
    }



    /**
     * 获取新框架登录信息
     * @return
     */
    @GetMapping("/getInfo")
    public Result<User> getInfo(){
        User user = sysLoginService.getInfo();

//        String token = SecurityUtils.getToken();
//        String cusUserJSONString = redisTemplate.opsForValue().get(token);
//        User user = JSONObject.parseObject(cusUserJSONString, User.class);
        return Result.ok(user);
    }



    //    @PostMapping("userLogin")
//    public R<?> userLogin(@RequestBody UserLogin user) {
//        // 用户登录
//        // 获取登录token
//        return R.ok(sysLoginService.userLogin(user.getUsername(), user.getPassword()));
//    }



    //    /**
//     * 获取用户信息
//     * @return
//     */
//    @GetMapping("/getInfo")
//    public R<?> getInfo() {
//        return R.ok(sysLoginService.getInfo());
//    }





}
